package com.qdaily.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.qdaily.supportlibrary.app.CubeFragment;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.ui.net.QdailyNetwork;
import com.qdaily.ui.util.AnalyticsHelper;
import com.qdaily.ui.util.BusProvider;
import com.qdaily.ui.util.ToastUtil;

/**
 * Created by Felix on 4/1/15.
 */
public abstract class QDBaseFragment extends CubeFragment implements QdailyNetwork.OnRequestCallBackListener {

    private QdailyNetwork qdailyNetWork;

    protected final String QDTAG = getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Register ourselves so that we can provide the initial value.
        BusProvider.getInstance().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(getLayoutId(), null);
        setupViews(contentView);
        setupData();
        return contentView;
    }

    @Override
    public void onResume() {
        AnalyticsHelper.getInstance().onResumed(getContext(), getClass().getName());

        super.onResume();
    }

    @Override
    public void onPause() {
        AnalyticsHelper.getInstance().onPaused(getContext(), getClass().getName());

        super.onPause();
    }

    @Override
    public void onDestroy() {
        // Always unregister when an object no longer should be on the bus.
        BusProvider.getInstance().unregister(this);

        super.onDestroy();
    }

    public QdailyNetwork getRequestManager() {
        if (qdailyNetWork == null) {
            qdailyNetWork = new QdailyNetwork(getContext(), this);
        }
        return qdailyNetWork;
    }

    /**
     * 为了有个base容器做的封装，暂没用。FUCK this
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    protected abstract int getLayoutId();

    protected abstract void setupViews(View view);

    protected abstract void setupData();

    @Override
    public void onResponse(NetUtil.NetParams params, JsonData response) {
        onRequestCallBack(params, response);
    }

    @Override
    public void onResponseError(NetUtil.NetParams params, Object response) {
        onRequestErrorCallBack(params, response);
    }

    @Override
    public void onResponseCache(NetUtil.NetParams params, JsonData response) {

    }

    /**
     * 吐司弹窗
     */
    public void displayToast(String toastConetnt) {
        ToastUtil.showToast(getContext(), toastConetnt);
    }

    public void displayToast(int toastConetntRes) {
        ToastUtil.showToast(getContext(), toastConetntRes);
    }

    /**
     * @param params   请求时候发出的参数
     * @param response 回调数据
     * @Desription 服务接口回调
     */
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
    }

    protected void onRequestErrorCallBack(NetUtil.NetParams params, Object response) {
    }
}
